//
//  REDateViewController.m
//  录音
//
//  Created by ywy on 2016/12/26.
//  Copyright © 2016年 Wely. All rights reserved.
//

#import <MJRefresh.h>
#import <SVProgressHUD/SVProgressHUD.h>
#import "REDateViewController.h"
#import "REdateTableViewCell.h"// cell
#import "RESingleton.h"
#import "RECheckLogin.h"
#import "REfirstCategory.h" //归类提示VIew
#import "REClassifyLocalFileViewController.h"// 归类的界面
#import "RETabBarViewController.h"
#import <AVFoundation/AVFoundation.h>
struct PlayItemInfo{
    float total;
    float currentTime;
};
@interface REDateViewController ()<UITableViewDelegate,UITableViewDataSource,AVAudioPlayerDelegate,UIAlertViewDelegate>
{
     UIView * _baseView;
    REfirstCategory * _firstCategory;
}
@property(nonatomic,strong)NSMutableArray * dataArr;
@property(nonatomic,strong)  UIDatePicker *datePicker;
@property (nonatomic,strong)ReRecordModel * recordModel;
@property(nonatomic,strong) REdateTableViewCell * baseCell;
@property (nonatomic, strong) NSIndexPath *indexPath;

@property (nonatomic, strong) AVPlayer     *selfPlayer;
@property (nonatomic, assign) CMTime currentTime;
@property(nonatomic,assign) BOOL ispaly;
@property (nonatomic,assign) CGFloat progress;
@property (nonatomic,copy) NSString * progressStr;


@property (nonatomic, strong) id timeObserver;

@property(nonatomic,strong) ReRecordModel * baseMode;

@end

@implementation REDateViewController

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    RETabBarViewController * tabBar = (RETabBarViewController *)self.tabBarController;
    //自定义隐藏Tabbar
    [tabBar HideTabarView:NO];
    
//    self.title = @"帮助与反馈";
    UIImageView * firstImageView  =[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"帮助与反馈"]];
    firstImageView.frame = CGRectMake(0, 0, 37, 17);
    self.navigationItem.titleView=firstImageView;
    // updateTheDatabase
    [[NSNotificationCenter defaultCenter]  addObserver:self selector:@selector(upDateTheDatabseClick) name:@"updateTheDatabase" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reFreshClicK:)  name:@"reFresh" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(stopPlayNoti)  name:@"stopPlay" object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(stopPlayNotiFirsrt) name:@"stopPlayFirst" object:nil];
    
    if (self.dataArr.count) {
        [self.dataArr removeAllObjects];
    }
    NSArray * arr;
    if (self.datelabel.text) {
        arr = [[RESingleton defaultCent] readModelWithDate:self.datelabel.text];
    }else
    {
        arr  = [[RESingleton defaultCent] readModelWithDate:[RECheckLogin stringFromDate:[NSDate date]]];
    }
    self.dataArr = [NSMutableArray new];
    for (ReRecordModel* model in arr) {
        [self.dataArr addObject:model];
    }
    [self requestBaseUrl];

     [self.baseTabelView.mj_header beginRefreshing];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(Update) name:@"Update" object:nil];
    
    self.ispaly = NO;
    self.indexPath = nil;
    self.datelabel.font = [UIFont fontWithName:@"MyriadPro-BoldCond" size:28];
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(selectedDateCLick)];
    [self.datelabel addGestureRecognizer:tap];
    _firstCategory = [[REfirstCategory alloc] initWithFrame:CGRectMake(0, 0, REScreenW, REScreenH)];
    _firstCategory.hidden = YES;
    [_firstCategory.deleteButton addTarget:self action:@selector(deleteClick) forControlEvents:UIControlEventTouchUpInside];
    [_firstCategory.popButton addTarget:self action:@selector(popClick) forControlEvents:UIControlEventTouchUpInside];
    [[UIApplication sharedApplication].keyWindow.rootViewController.view addSubview:_firstCategory];
    [self addSubViewBack];
    self.dateView.layer.borderColor = [UIColor redColor].CGColor;
    self.baseTabelView.delegate = self;
    self.baseTabelView.dataSource = self;
    self.baseTabelView.separatorStyle = 0;
    UIView * footView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, REScreenW, 60)];
    self.baseTabelView.tableFooterView =footView;
    [self.baseTabelView registerNib:[UINib nibWithNibName:@"REdateTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"REdateTableViewCell"];
    self.datelabel.text = [RECheckLogin stringFromDate:[NSDate date]];
    self.baseTabelView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        dispatch_async(dispatch_get_global_queue(0, 0), ^{
            NSArray * arr;
            if (self.datelabel.text) {
                arr = [[RESingleton defaultCent] readModelWithDate:self.datelabel.text];
            }else
            {
                arr  = [[RESingleton defaultCent] readModelWithDate:[RECheckLogin stringFromDate:[NSDate date]]];
            }
            self.dataArr = [NSMutableArray new];
            for (ReRecordModel* model in arr) {
                [self.dataArr addObject:model];
            }
                [self requestBaseUrl];
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.baseTabelView reloadData];
                [self.baseTabelView.mj_header endRefreshing];
            });
        });
    }];
    [self.baseTabelView.mj_header beginRefreshing];
  
}
-(void)Update
{
    NSArray * arr;
    if (self.datelabel.text) {
        arr = [[RESingleton defaultCent] readModelWithDate:self.datelabel.text];
    }else
    {
        arr  = [[RESingleton defaultCent] readModelWithDate:[RECheckLogin stringFromDate:[NSDate date]]];
    }
    self.dataArr = [NSMutableArray new];
    for (ReRecordModel* model in arr) {
        [self.dataArr addObject:model];
    }
    [self requestBaseUrl];
    
    [self.baseTabelView.mj_header beginRefreshing];
     [[NSNotificationCenter defaultCenter] removeObserver:self name:@"Update" object:nil];

}

-(void)reFreshClicK:(NSNotification*)notification
{
    NSLog(@"%@ === %@ === %@", notification.object, notification.userInfo, notification.name);
    
    NSDictionary * dict = notification.userInfo;
    
    if ([dict[@"number"] isEqualToString:@"0"]) {
        [self.baseTabelView .mj_header beginRefreshing];
    }
    
}

-(void)deleteClick
{
    _firstCategory.hidden = YES;
}
-(void)popClick
{
     _firstCategory.hidden = YES;
    // 归类
    REClassifyLocalFileViewController * classifyLocalVC =[[REClassifyLocalFileViewController alloc] init];
    classifyLocalVC.baseModel = self.recordModel;
    [self.navigationController pushViewController:classifyLocalVC animated:YES];
}


-(void)addSubViewBack
{
    _baseView = [[UIView alloc] initWithFrame:self.view.bounds];
    _baseView.backgroundColor = halfOfAlpha;
    UIButton * backButton = [UIButton buttonWithType:UIButtonTypeSystem];
    backButton.frame = self.view.bounds;
    [backButton addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
    [_baseView addSubview:backButton];
    // 创建一个日期选择器
    self.datePicker = [[UIDatePicker alloc] init];
    self.datePicker.backgroundColor = [UIColor grayColor];
    self.datePicker.frame = CGRectMake(0, REScreenH-216, REScreenW, 216);
    //en_US  zh_CN
    //设置显示日期的本地化 ([NSLocale currentLocale] 获取当前系统的本地化)
    //datePicker.locale = [NSLocale currentLocale] ;
    //修改本地化 设置为 中文
    self.datePicker.locale = [NSLocale localeWithLocaleIdentifier:@"zh_CN"] ;
    //设置日期选择器的 类型
    self.datePicker.datePickerMode = UIDatePickerModeDate;
    //根据上面设置的类型 设置具体的时间
    [self.datePicker setDate:[NSDate date] animated:YES];
    //增加 事件
    //当滚动 picker 的时候调用 showDate
    [self.datePicker addTarget:self action:@selector(showDate:) forControlEvents:UIControlEventValueChanged];
    [_baseView addSubview:self.datePicker];
    _baseView.hidden = YES;
    [[UIApplication sharedApplication].keyWindow.rootViewController.view addSubview:_baseView];
}
-(void)btnClick
{
    _baseView.hidden = YES;
}
-(void)selectedDateCLick
{
    _baseView.hidden = NO;
}

- (void)showDate:(UIDatePicker *)datePicker
{
    self.datelabel.text =  [RECheckLogin stringFromDate:datePicker.date];
    NSArray * arr  = [[RESingleton defaultCent] readModelWithDate:self.datelabel.text];
    self.dataArr = [NSMutableArray new];
    for (ReRecordModel* model in arr) {
        [self.dataArr addObject:model];
    }
    [self.baseTabelView reloadData];
}
-(void)requestBaseUrl
{
    __weak typeof(self) weakSelf = self;
    NSMutableDictionary * dict = [NSMutableDictionary new];
    dict[@"cmd"] = @"getLocalAlbumList";
    dict[@"uid"] = [UserDefaults objectForKey:@"userid"];
//    dict[@"date"] =[RECheckLogin stringFromDateFirst:[NSDate date]];
    dict[@"date"] = [RECheckLogin getDataStr:self.datelabel.text andData:[NSDate date]];
    [RECheckLogin POST:REBaseUrl parameters:dict progress:^(CGFloat prosser) {
        
    } success:^(id responseObject) {
        if ([responseObject[@"result" ] isEqualToString:@"0"] ) {
            weakSelf.totleTimeLabel.text = [NSString stringWithFormat:@"当日总录制时间:%@",responseObject[@"recTimeLength"]];
             weakSelf.totleTimeTwoLabel.text = [NSString stringWithFormat:@"当日文件总时长:%@",responseObject[@"fileTimeLength"]];
        }else{
//            [SVProgressHUD showErrorWithStatus:responseObject[@"resultNote"]];
        }
    } failure:^(NSError *error) {
        [SVProgressHUD showErrorWithStatus:@"网络加载错误"];
    }];
}


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArr.count ;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 140;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    __weak typeof(self) weakSelf = self;
    REdateTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"REdateTableViewCell" forIndexPath:indexPath];
    ReRecordModel * mdeol = self.dataArr[indexPath.row];
    cell.indexPath  = indexPath;
    [cell showDate:mdeol withIndexPath:_indexPath];
    
    if (![self.indexPath isEqual:indexPath] ){
        cell.progressView.progress = 0;
        cell.progressLabel.text = @"00:00";
        
    }else{
        
        cell.progressView.progress = self.progress;
        cell.progressLabel.text = self.progressStr ? self.progressStr  : @"00:00";
        
    }
    cell.palyButton.selected = [self.indexPath isEqual:indexPath] && self.ispaly;
    
    cell.play = ^(ReRecordModel * baseModel,BOOL playOrStop,NSInteger number,UITableViewCell * baseCell,NSIndexPath * indexPathOfBlock)
    {
        weakSelf.baseMode = baseModel;
        switch (number) {
            case 10001:
            {
                YPLog(@"选中的cell %@",indexPathOfBlock);
                weakSelf.baseCell = [weakSelf.baseTabelView cellForRowAtIndexPath:_indexPath];
                [weakSelf palyAndStop:playOrStop withModel:baseModel andIndexPath:indexPath seleCted:_indexPath];
            }
                break;
            case 10002:
            {
                UIAlertView * titleAlert = [[UIAlertView alloc] initWithTitle:@"客官不要啊！！" message:@"客官确定要删除？" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
                [titleAlert show];
            }
                break;
            case 10003:
            {
                if (![baseModel.isUpPost isEqualToString:@"noUp"]) {
                    [SVProgressHUD showErrorWithStatus:@"曲目已上传"];
                    return;
                }else
                {// 上传
                if (![baseModel.isClas isEqualToString:@"noClass"]) {
                // 已经归类 开始上传
                    [weakSelf postFilewith:baseModel];
                }else
                {
                    _firstCategory.hidden = NO;
                    weakSelf.recordModel = baseModel;
                    }
                }
            }
                break;
            case 10004:
            {
                
                //归类
                REClassifyLocalFileViewController * classifyLocalVC =[[REClassifyLocalFileViewController alloc] init];
                classifyLocalVC.noclassStr = baseModel.isClas;
                classifyLocalVC.baseModel = baseModel;
                [weakSelf.navigationController pushViewController:classifyLocalVC animated:YES];
                
                
            }
                break;
            default:
                break;
        }
           };
    cell.selectionStyle = 0;
    return cell;
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1) {
        //删除
        BOOL blHave=[[NSFileManager defaultManager] fileExistsAtPath: [RECheckLogin getlocalPath:@"/" allStr:self.baseMode.baseUrl]];
        
        if (!blHave) {
            [SVProgressHUD showErrorWithStatus:@"文件不存在"];
            return ;
        }else {
            
            BOOL blDele= [[NSFileManager defaultManager] removeItemAtPath:[RECheckLogin getlocalPath:@"/" allStr:self.baseMode.baseUrl]  error:nil];
            if (blDele) {
                [SVProgressHUD showSuccessWithStatus:@"文件删除成功!!!!"];
                [[RESingleton defaultCent] deleteModel:self.baseMode.baseUrl];
                [self.baseTabelView.mj_header beginRefreshing];
            }else {
                [SVProgressHUD showErrorWithStatus:@"文件删除失败!!!!"];
            }
        }

    }
}

-(void)palyAndStop:(BOOL)playOrStop  withModel:(ReRecordModel*)baseModel andIndexPath:(NSIndexPath*)indexPath seleCted:(NSIndexPath*)indexPathOfBlock
{
    if (![self.indexPath isEqual:indexPath]){
        [self initPalyerWithModel:baseModel];
    }else{
        if (!self.ispaly) {
            REdateTableViewCell * cell = [self.baseTabelView cellForRowAtIndexPath:indexPath];
            if (cell.progressView.progress <= 0) {
                [self.selfPlayer seekToTime:CMTimeMake(0, 1)];
            }
            [self.selfPlayer play];
            
            self.ispaly = YES;
        }else{
            [self.selfPlayer pause];
            self.ispaly = NO;
        }
    }
    
    self.indexPath = indexPath;
    [self.baseTabelView reloadData];
}
-(void)initPalyerWithModel:(ReRecordModel*)baseModel{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:AVPlayerItemDidPlayToEndTimeNotification object:nil];
    [self.selfPlayer.currentItem removeObserver:self  forKeyPath:@"status"];
    [self.selfPlayer removeTimeObserver:self.timeObserver];
    
    AVAudioSession *session = [AVAudioSession sharedInstance];
    [session overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker error:nil];
    
//    [session setCategory:AVAudioSessionCategoryPlayback error:nil];
    
    [session setActive:YES error:nil];
    NSURL *localURL = [NSURL fileURLWithPath:[RECheckLogin getlocalPath:@"/" allStr:baseModel.baseUrl]];
    AVURLAsset *localAssert = [[AVURLAsset alloc]initWithURL:localURL options:nil];
    //AVPlayerItem 他是对assert对应资源的一种整体描述，包括能够播放，以及影片长度等内容
    AVPlayerItem * item = [[AVPlayerItem alloc]initWithAsset:localAssert];
    //使用item生成player，这里会对影片进行预加载分析，然后更新item的状态
    //            self.selfPlayer.volume = 1;
    if(self.selfPlayer){
        [self.selfPlayer pause];
        self.selfPlayer = nil;
    }
    self.selfPlayer = [AVPlayer playerWithPlayerItem:item];
    //监听AVPlayerItem的status状态，当status状态为readyToPlayer时，我们认为影片时可以播放的
    [item addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionNew context:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playbackFinished) name:AVPlayerItemDidPlayToEndTimeNotification object:self.selfPlayer.currentItem];
}
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    __weak typeof(self) weakSelf = self;
    if ([keyPath isEqualToString:@"status"]) {
        AVPlayerItem *item = (AVPlayerItem*)object;
        if (item.status == AVPlayerItemStatusReadyToPlay) {
            //这里影片可以播放
            [self.selfPlayer play];
            self.ispaly = YES;
        weakSelf.timeObserver =  [weakSelf.selfPlayer addPeriodicTimeObserverForInterval:CMTimeMake(1, 1) queue:dispatch_get_main_queue() usingBlock:^(CMTime time) {
                //time是指当前影片播放的时间
                //得到影片的总时间
                CMTime totalTime = weakSelf.selfPlayer.currentItem.duration;
                //CMTimeGetSeconds 把CMTime转换为秒数
                CGFloat currentTimeSeconds = CMTimeGetSeconds(time);
                CGFloat totalSeconds = CMTimeGetSeconds(totalTime);
                int  minnute = currentTimeSeconds/60;
                int second = currentTimeSeconds-minnute*60;
                REdateTableViewCell * cell = [weakSelf.baseTabelView cellForRowAtIndexPath:weakSelf.indexPath];
                weakSelf.progressStr = [NSString stringWithFormat:@"%02d:%02d",minnute,second];
                weakSelf.progress = currentTimeSeconds*1.0/totalSeconds;
                if (cell){
                    cell.progressLabel.text = weakSelf.progressStr;
                     [cell.progressView setProgress:weakSelf.progress animated:NO];
                }
            }];
            [self.baseTabelView reloadData];
        }else{
            [SVProgressHUD showErrorWithStatus:@"录音文件暂时无法播放"];
        }
    }
}
static dispatch_source_t _timer;

-(void)creatCurrentTIme:(int)value
{
    
    __block int64_t timeout = value; //倒计时时间
    __weak typeof(self) weakSelf = self;
    if (_timer != nil){
        dispatch_source_cancel(_timer);
    }
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    _timer  = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
    dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
    dispatch_source_set_event_handler(_timer, ^{
        if(timeout<0){ //倒计时结束，关闭
            dispatch_source_cancel(_timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                //刷新主页(由于服务器时间误差,延时刷新)
            });
        }else{
            dispatch_async(dispatch_get_main_queue(), ^{
                if ( self.ispaly == NO) {
                    
                    return;
                }else
                {
              
                    int  minnute =(value- (int)timeout)/60;
                    int second = (value-(int)timeout)-minnute*60;
                    REdateTableViewCell * cell = [weakSelf.baseTabelView cellForRowAtIndexPath:weakSelf.indexPath];
                    weakSelf.progressStr = [NSString stringWithFormat:@"%02d:%02d",minnute,second];
                    weakSelf.progress =1- timeout*1.0/value;
                    if (cell){
                        cell.progressLabel.text = weakSelf.progressStr;
                        [cell.progressView setProgress:weakSelf.progress animated:NO];
                    }

                }
        });
            timeout--;
        }
    });
    dispatch_resume(_timer);
}

-(void)playbackFinished{
    
    REdateTableViewCell * cell = [self.baseTabelView cellForRowAtIndexPath:self.indexPath];
    if (cell){
        cell.progressView.progress = 0.0;
        cell.progressLabel.text = @"00:00";
        self.progress = 0.0;
        self.progressStr = @"00:00";
        [cell.palyButton setSelected:NO];
    }
    self.ispaly = NO;
    
}

// 昨天
- (IBAction)beforeClick:(UIButton *)sender {
    self.datelabel.text = [RECheckLogin  stringFromDateStr:self.datelabel.text withAdd:NO];
    NSArray * arr  = [[RESingleton defaultCent] readModelWithDate:self.datelabel.text];
    self.dataArr = [NSMutableArray new];
    for (ReRecordModel* model in arr) {
        [self.dataArr addObject:model];
    }
    [self requestBaseUrl];
    [self.baseTabelView reloadData];
    
}

// 明天
- (IBAction)nextClick:(UIButton *)sender {
      self.datelabel.text = [RECheckLogin  stringFromDateStr:self.datelabel.text withAdd:YES];
    NSArray * arr  = [[RESingleton defaultCent] readModelWithDate:self.datelabel.text];
    self.dataArr = [NSMutableArray new];
    for (ReRecordModel* model in arr) {
        [self.dataArr addObject:model];
    }
     [self requestBaseUrl];
    [self.baseTabelView reloadData];
}




-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    
    if (self.selfPlayer) {
        REdateTableViewCell * cell = [self.baseTabelView cellForRowAtIndexPath:self.indexPath];
        self.ispaly = NO;
        [cell.palyButton setSelected:NO];
        [self.selfPlayer pause];
    }
//    self.selfPlayer = nil;
}
-(void)stopPlayNoti
{
    if (self.selfPlayer) {
        REdateTableViewCell * cell = [self.baseTabelView cellForRowAtIndexPath:self.indexPath];
        self.ispaly = NO;
        [cell.palyButton setSelected:NO];
        [self.selfPlayer pause];
    }
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"stopPlayNoti" object:nil];
}
-(void)stopPlayNotiFirsrt
{
    if (self.selfPlayer) {
        REdateTableViewCell * cell = [self.baseTabelView cellForRowAtIndexPath:self.indexPath];
        self.ispaly = NO;
        [cell.palyButton setSelected:NO];
        [self.selfPlayer pause];
    }
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"stopPlayNotiFirsrt" object:nil];
}


-(void)upDateTheDatabseClick
{
    [self.baseTabelView.mj_header beginRefreshing];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"updateTheDatabase" object:nil];
}

-(void)postFilewith:(ReRecordModel*)model
{
    __weak typeof(self) weakSelf = self;
    NSMutableDictionary * dict = [NSMutableDictionary new];
    dict[@"cmd"] = @"uploadMusic";
    dict[@"uid"] = [UserDefaults objectForKey:@"userid"];
    dict[@"firstTypeId"] = model.firstStr;
    dict[@"secondTypeId"] = model.secondStr;
    dict[@"thirdTypeId"] = model.thirdStr;
    dict[@"albumLength"] = model.currentTime;
    dict[@"albumName"] = model.titleStr;
    NSString * filename = [NSString stringWithFormat:@"%@.caf",[RECheckLogin stringFromDateTwo:[NSDate date]]];
    dict[@"file"] = filename;
    dict[@"localId"] = model.baseUrl;
    [SVProgressHUD showWithStatus:@"曲目上传中...."];
    [RECheckLogin POSTTwo:REBaseUrl parameters:dict withFormData:^(id form) {
     
     [form appendPartWithFileURL:[NSURL fileURLWithPath:[RECheckLogin getlocalPath:@"/" allStr:model.baseUrl]] name:@"file" fileName:filename mimeType:@"file" error:nil];
        } progress:^(CGFloat prosser) {
       } success:^(id responseObject) {
           if ([responseObject[@"result"] isEqualToString:@"0"]) {
                 [SVProgressHUD showWithStatus:@"曲目上传成功"];
               dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                   [SVProgressHUD dismiss];
               });
               [[NSNotificationCenter defaultCenter] postNotificationName:@"postSucces" object:nil];
               // 已经上传的 时候 需要更新数据库的信息
               if (  [[RESingleton defaultCent] isUp:model.baseUrl] == YES && [[RESingleton defaultCent] isIdStr:responseObject[@"mid"] with:model.baseUrl]) {
                   [weakSelf .baseTabelView.mj_header beginRefreshing];
               }else
               {
//                   [SVProgressHUD showErrorWithStatus:@"更新数据失败!!!!"];
               }
               
           }else
           {
//               [SVProgressHUD showErrorWithStatus:@"上传曲目失败"];
           }
             [SVProgressHUD dismiss];
       } failure:^(NSError *error) {
           
          [SVProgressHUD showErrorWithStatus:@"网络加载失败!!!"];
    }];
    
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];

}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
